﻿<cfsilent>
	<cfsetting requesttimeout="90" />
	<cfscript>

		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		sessionAdvice = getProperty("serviceFactory").getBean("sessionAdvice");
		datetimeAdvice = getProperty("serviceFactory").getBean("datetimeAdvice");

		chooseProp = getProperty("serviceFactory").getBean("choosePropertyAdvice");
		taskProp = getProperty("serviceFactory").getBean("taskPropertyAdvice");
		turnProp = getProperty("serviceFactory").getBean("turnPropertyAdvice");

		termId = event.getArg("TermID");
		courseId = event.getArg("CID");
		courseIdx = event.getArg("CIDX");
		taskType = event.getArg("TT");
		taskId = event.getArg("TSK");
		chooserId = event.getArg("SCC");

		secureTicket = event.getArg("ST");
		
		turnId = event.getArg("CT");

		/* 学籍相关信息 */
		studentId = sessionAdvice.getAutherUserID();
		//studentClassId = sessionAdvice.getUserProp("studentClass");
		studentSubjectId = sessionAdvice.getUserProp("studentSubject");
		//studentDirId = sessionAdvice.getUserProp("studentSubDir");
		studentGrade = sessionAdvice.getUserProp("studentGrade");
		studentDepartment = sessionAdvice.getUserProp("studentDepartment");

		chooserSecure = false;
		chooserAllowed = false;
		
		/* 回执信息 */
		response = structNew();
		structInsert(response, "success", false, true);
		
		
		/* 首先做安全检查 */
		try {
			stringOrignal = decrypt(secureTicket, taskId, "PBEWithMD5AndDES", "Hex");
			stringTest = courseId & courseIdx & termId & taskType;
			
			if ( stringOrignal eq stringTest ) {
				chooserSecure = true;
			}
		}
		catch(any excpt) {
		}
		
		if ( chooserSecure ) {

			/* 检查选课轮次 */
			if ( len(turnId) ) {

				sql = "SELECT 
							date_start,date_end,time_start,time_end,
							grade_allowed,institute_allowed,subject_allowed,turn_prop 
						FROM 
							t_choose_turn 
						WHERE 
							ct = :turnId ";
				queryObj = new query( datasource=application.dnsSlave );
				queryObj.addParam( name="turnId", value=turnId, cfsqltype="cf_sql_char" );
				rs_turn = queryObj.execute( sql=sql ).getResult();
			
				if ( rs_turn.recordCount gt 0 ) {

					today = datetimeAdvice.getCurrentDateString();
					currentTime = TimeFormat(now(), "HHMM");
				
					if ( rs_turn.date_start lte today and today lte rs_turn.date_end ) {
						
						if ( rs_turn.time_start lte currentTime and currentTime lte rs_turn.time_end ) {
							if ( rs_turn.grade_allowed eq "ALL" or listFind(rs_turn.grade_allowed, studentGrade, ",") ) {
								if ( rs_turn.institute_allowed eq "ALL" or listFind(rs_turn.institute_allowed , studentDepartment, ",") or (rs_turn.subject_allowed neq '-' and listFind(rs_turn.subject_allowed, studentSubjectId)) ) {
									/* 是否开放计划课程选课 */
									turnProp.parseProperty(rs_turn.turn_prop);
									if ( turnProp.getProgramTask() ) {
										chooserAllowed = true;
									}
								}
							}
						}
					}
				}
			}
			
			if ( chooserAllowed ) {
				
				transaction {
					
					
					switch(taskType){
						case "C":
							sql = "SELECT task_prop 
									FROM t_common_task 
									WHERE tsk_id = :taskId ";
							break;
			
						case "P":
							sql = "SELECT task_prop 
									FROM t_task 
									WHERE tsk_id = :taskId ";
							break;
						
						case "R":
							sql = "SELECT task_prop 
									FROM t_reopen_task 
									WHERE tsk_id = :taskId ";
							break;
						
						case "T":
							sql = "SELECT task_prop 
									FROM t_sport_task  
									WHERE tsk_id = :taskId ";
							break;
					}
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
					rs_task = queryObj.execute( sql=sql ).getResult();

					
					taskProp.parseProperty( rs_task.task_prop );
					
					sql = "SELECT choose_prop 
							FROM t_student_course 
							WHERE scc_id = :chooserId ";

					queryObj = new query( datasource=application.dnsMaster );

					queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );

					rs_studentCourse = queryObj.execute( sql=sql ).getResult();
					
					chooseProp.parseProperty( rs_studentCourse.choose_prop );

					if ( taskProp.getDischarge() ) {
						structInsert(response, "reason", "教学班禁止退课", true);
					}
					else if ( chooseProp.getLocked() ) {
						structInsert(response, "reason", "选课记录禁止修改", true);
					} 
					else {

						/* 增加选课席位 */
						switch (taskType) {
								
							case "C":
								sql = "update t_common_task 
										SET task_chooesd = task_chooesd - 1  
										WHERE tsk_id = :taskId ";
								break;
				
							case "P":
								sql = "update t_task 
										SET task_chooesd = task_chooesd - 1  
										WHERE tsk_id = :taskId ";
								break;
							
							case "R":
								sql = "update t_reopen_task 
										SET task_chooesd = task_chooesd - 1  
										WHERE tsk_id = :taskId ";
								break;
							
							case "T":
								sql = "update t_sport_task 
										SET task_chooesd = task_chooesd - 1  
										WHERE tsk_id = :taskId ";
								break;
						}
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
						queryObj.execute( sql=sql );

					
						sql = "DELETE FROM t_student_course 
								WHERE scc_id = :chooserId ";
						queryObj = new query( datasource=application.dnsMaster );
						queryObj.addParam( name="chooserId", value=chooserId, cfsqltype="cf_sql_varchar" );
						queryObj.execute( sql=sql );
						
						structInsert(response, "success", true, true);
					
					}

				}
			}
			else {
				structInsert(response, "reason", "现在不是选课时段", true);
			}
		}
		else {
			structInsert(response, "reason", "参数错误", true);
		}
		
			
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>